package com.elvt.server.config.security;

import com.elvt.server.utils.RespSult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 作者： elvt
 * @Data 时间：2022/5/22
 * @packageName 包路径： ybg_back_end
 * @Desciption 说明： 当访问接口没有权限时
 *
 */
@Component

public class RestfulAccessDeinedHand implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
       response.setCharacterEncoding("UTF-8");
       response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        RespSult bean = RespSult.error("权限不足，请联系管理员!");
        out.write(new ObjectMapper().writeValueAsString(bean));
        out.flush();
        out.close();



    }
}
